package cc.vv.baselibrary.view.calendar;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.util.BaseTimeUtil;
import cc.vv.baselibrary.util.LKDialogUtil;
import cc.vv.baselibrary.util.LKLogUtil;
import cc.vv.baselibrary.view.WindowBottomDialog;
import cc.vv.baselibrary.view.calendar.month.MonthCalendarView;

/**
 * 公共日历选择控件
 * Created by sun.li on 2018/8/13.
 *
 * @author sun.li
 */

public class BaseCalendarViewUtil {

    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 年，月，日
     */
    private int yearCurrent;
    private int monthCurrent;
    private int dayCurrent;

    private TextView tv_data;

    /**
     * 一周含有的日期,倒序
     */
    ArrayList<String> strList = new ArrayList<>();

    private WindowBottomDialog windowBottomDialog;

    public BaseCalendarViewUtil(Context context) {
        mContext = context;
    }

    /**
     * 显示日期Dialog
     *
     * @param type 类型 0：日；1：周；2：月；
     */
    public void showCalendarDialog(final int type, final DateSelectedEvent dateSelectedEvent, final HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> yearHashMap) {
        if (null == mContext) {
            return;
        }
        try {
            View view1 = LayoutInflater.from(mContext).inflate(R.layout.view_calendar_layout, null);
            windowBottomDialog = (WindowBottomDialog) LKDialogUtil.getBottomDialog((Activity) mContext, view1);
            tv_data = (TextView) view1.findViewById(R.id.tv_clv_data);
            String strTime = BaseTimeUtil.getStrTime(System.currentTimeMillis(), "yyyy-MM-dd");
            String[] split = strTime.split("-");
            tv_data.setText(split[0] + "年" + split[1] + "月" + split[2] + "日");
            final MonthCalendarView mcvCalendar = (MonthCalendarView) view1.findViewById(R.id.mcv_clv_calendar);
            mcvCalendar.setOnCalendarClickListener(new OnCalendarClickListener() {
                @Override
                public void onClickDate(int year, int month, int day) {
                    yearCurrent = year;
                    monthCurrent = month;
                    dayCurrent = day;
                    tv_data.setText(yearCurrent + "年" + monthCurrent);
                    strList.clear();
                    ArrayList<String> strings = CalendarUtils.convertWeekByDate(year + "-" + month + "-" + day);
                    strList.addAll(strings);
                }

                @Override
                public void onPageChange(int year, int month, int day) {
                    if (null != dateSelectedEvent) {
                        dateSelectedEvent.onPageChange(year, month, day);
                    }
                    if (yearHashMap != null) {
                        if (yearHashMap.size() > 0) {
                            if (yearHashMap.get(year) != null) {
                                if (yearHashMap.get(year).get(month + 1) != null && yearHashMap.get(year).get(month + 1).size() > 0) {
                                    mcvCalendar.getCurrentMonthView().setTaskHintList(yearHashMap.get(year).get(month + 1));
                                }
                            }
                        }
                    }
                }
            });
            TextView tv_confirm = (TextView) view1.findViewById(R.id.tv_schedule_clv_confirm);
            TextView tv_cancel = (TextView) view1.findViewById(R.id.tv_schedule_clv_cancel);
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    windowBottomDialog.hide();
                    //日期格式2017-07-12 00:00:00,参数需要处理
                    String startDate = yearCurrent + "-" + monthCurrent + "-" + dayCurrent + " 00:00:00";
                    String endDate = yearCurrent + "-" + monthCurrent + "-" + dayCurrent + " 23:59:59";
                    if (type == 0) {
                        LKLogUtil.e("=======DstartDate" + startDate);
                        LKLogUtil.e("=======DendDate" + endDate);
                    } else if (type == 1) {
                        if (strList.size() == 0) {
                            return;
                        }
                        String s = strList.get(0);
                        String[] date = s.split("-");
                        String endDateText = "";
                        if (date.length > 2) {
                            endDateText = date[1] + "-" + date[2];
                        }
                        //日期格式2017-07-12 00:00:00
                        startDate = strList.get(6) + " 00:00:00";
                        endDate = strList.get(0) + " 23:59:59";
                        LKLogUtil.e("=======MstartDate" + startDate);
                        LKLogUtil.e("=======MendDate" + endDate);
                    } else if (type == 2) {
                        //                    textViewStart.setText(yearCurrent + "." + monthCurrent);
                    }
                    if (null != dateSelectedEvent) {
                        dateSelectedEvent.confirmDate(yearCurrent, monthCurrent, dayCurrent);
                        if(startDate.equals("0-0-0 00:00:00")){
                            startDate = "";
                        }
                        dateSelectedEvent.confirmDate(startDate);
                    }
                }
            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    windowBottomDialog.hide();
                }
            });
            mcvCalendar.setCurrentItem(mcvCalendar.getCurrentItem() + 1);
            if (!windowBottomDialog.isShowing()) {
                windowBottomDialog.show();
                mcvCalendar.setCurrentItem(mcvCalendar.getCurrentItem() - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface DateSelectedEvent {

        /**
         * 确认时间"yyyy-MM-dd HH:mm:ss"
         */
        void confirmDate(String date);

        /**
         * 确认时间
         */
        void confirmDate(int year, int month, int day);

        void onPageChange(int year, int month, int day);
    }

    /**
     * 显示日期Dialog
     *
     * @param type 类型 0：日；1：周；2：月；
     */
    public void showCalendarDialogTake(final int type, final DateSelectedEvent dateSelectedEvent, final HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> yearHashMap, final String cancelText) {
        if (null == mContext) {
            return;
        }
        try {
            View view1 = LayoutInflater.from(mContext).inflate(R.layout.view_calendar_layout, null);
            windowBottomDialog = (WindowBottomDialog) LKDialogUtil.getBottomDialog((Activity) mContext, view1);
            tv_data = (TextView) view1.findViewById(R.id.tv_clv_data);
            String strTime = BaseTimeUtil.getStrTime(System.currentTimeMillis(), "yyyy-MM-dd");
            String[] split = strTime.split("-");
            tv_data.setText(split[0] + "年" + split[1] + "月" + split[2] + "日");
            final Integer year = Integer.valueOf(split[0]);
            final Integer month = Integer.valueOf(split[1]);
            final MonthCalendarView mcvCalendar = (MonthCalendarView) view1.findViewById(R.id.mcv_clv_calendar);
            mcvCalendar.setOnCalendarClickListener(new OnCalendarClickListener() {
                @Override
                public void onClickDate(int year, int month, int day) {
                    yearCurrent = year;
                    monthCurrent = month;
                    dayCurrent = day;
                    tv_data.setText(yearCurrent + "年" + monthCurrent);
                    strList.clear();
                    ArrayList<String> strings = CalendarUtils.convertWeekByDate(year + "-" + month + "-" + day);
                    strList.addAll(strings);
                }

                @Override
                public void onPageChange(int year, int month, int day) {
                    if (null != dateSelectedEvent) {
                        dateSelectedEvent.onPageChange(year, month, day);
                    }
                    if (yearHashMap.size() > 0) {
                        if (yearHashMap.get(year) != null) {
                            if (yearHashMap.get(year).get(month + 1) != null && yearHashMap.get(year).get(month + 1).size() > 0) {
                                mcvCalendar.getCurrentMonthView().setTaskHintList(yearHashMap.get(year).get(month + 1));
                            }
                        }
                    }
                }
            });
            TextView tv_confirm = (TextView) view1.findViewById(R.id.tv_schedule_clv_confirm);
            TextView tv_cancel = (TextView) view1.findViewById(R.id.tv_schedule_clv_cancel);
            tv_cancel.setText(cancelText);
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    windowBottomDialog.hide();
                    //日期格式2017-07-12 00:00:00,参数需要处理
                    String startDate = yearCurrent + "-" + monthCurrent + "-" + dayCurrent + " 00:00:00";
                    String endDate = yearCurrent + "-" + monthCurrent + "-" + dayCurrent + " 23:59:59";
                    if (type == 0) {
                        LKLogUtil.e("=======DstartDate" + startDate);
                        LKLogUtil.e("=======DendDate" + endDate);
                    } else if (type == 1) {
                        if (strList.size() == 0) {
                            return;
                        }
                        String s = strList.get(0);
                        String[] date = s.split("-");
                        String endDateText = "";
                        if (date.length > 2) {
                            endDateText = date[1] + "-" + date[2];
                        }
                        //日期格式2017-07-12 00:00:00
                        startDate = strList.get(6) + " 00:00:00";
                        endDate = strList.get(0) + " 23:59:59";
                        LKLogUtil.e("=======MstartDate" + startDate);
                        LKLogUtil.e("=======MendDate" + endDate);
                    } else if (type == 2) {
                        //                    textViewStart.setText(yearCurrent + "." + monthCurrent);
                    }
                    if (null != dateSelectedEvent) {
                        dateSelectedEvent.confirmDate(yearCurrent, monthCurrent, dayCurrent);
                        dateSelectedEvent.confirmDate(startDate);
                    }
                }
            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(cancelText.equals("全部")){
                        dateSelectedEvent.confirmDate("all");
                    }
                    windowBottomDialog.hide();
                }
            });
            mcvCalendar.setCurrentItem(mcvCalendar.getCurrentItem()+1);
            if (!windowBottomDialog.isShowing()) {
                windowBottomDialog.show();
                mcvCalendar.setCurrentItem(mcvCalendar.getCurrentItem()-1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
